/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entity.SystemM;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author ian
 */
public class OutlookControl {

    private static List temperature = new ArrayList();
    private static List presion = new ArrayList();
    private static List pluviosity = new ArrayList();
    private static OutlookControl instance;

    public OutlookControl() {
        //List<SystemM> statistics = view.CentralFrame.Registers;    
        init();
    }
    
    public static OutlookControl getInstance(){
        if(instance == null)
            instance = new OutlookControl();
        return instance;
    }

    private static void init() {
        for (int i = 0; i < view.CentralFrame.registers.size(); i++) {
            temperature.add(view.CentralFrame.registers.get(i).getTemperature());
            presion.add(view.CentralFrame.registers.get(i).getAtmPressure());
            pluviosity.add(view.CentralFrame.registers.get(i).getPluviosity());
        }
    }

    public static float getTemperatureMedia() {
        init();
        float media = 0;
        for (int i = 0; i < temperature.size(); i++) {
            media += (float) temperature.get(i);
        }
        media = media / temperature.size();

        return media;
    }

    public static float getPresionMedia() {
        init();
        float media = 0;
        for (int i = 0; i < presion.size(); i++) {
            media += (float) presion.get(i);
        }
        media = media / presion.size();

        return media;
    }

    public static float getPluviosityMedia() {
        init();
        float media = 0;
        for (int i = 0; i < pluviosity.size(); i++) {
            media += (float) pluviosity.get(i);
        }
        media = media / pluviosity.size();

        return media;
    }

    public static float getTemperatureModa() {
        init();
        int maxRept = 0;
        float moda = 0;

        for (int i = 0; i < temperature.size(); i++) {
            int rept = 0;
            for (int j = 0; j < temperature.size(); j++) {
                if (temperature.get(i) == temperature.get(j)) {
                    rept++;
                }
                if (rept > maxRept) {
                    moda = (float) temperature.get(i);
                    maxRept = rept;
                }
            }
        }
        return moda;
    }

    public static float getPresionModa() {
        init();
        int maxRept = 0;
        float moda = 0;

        for (int i = 0; i < presion.size(); i++) {
            int rept = 0;
            for (int j = 0; j < presion.size(); j++) {
                if (presion.get(i) == presion.get(j)) {
                    rept++;
                }
                if (rept > maxRept) {
                    moda = (float) presion.get(i);
                    maxRept = rept;
                }
            }
        }
        return moda;
    }

    public static float getPluviosityModa() {
        init();
        int maxRept = 0;
        float moda = 0;

        for (int i = 0; i < pluviosity.size(); i++) {
            int rept = 0;
            for (int j = 0; j < pluviosity.size(); j++) {
                if (pluviosity.get(i) == pluviosity.get(j)) {
                    rept++;
                }
                if (rept > maxRept) {
                    moda = (float) pluviosity.get(i);
                    maxRept = rept;
                }
            }
        }
        return moda;
    }

    public static float getTemperatureMediana() {
        init();
        float mediana = 0;
        Collections.sort(temperature);
        mediana = (float) temperature.get((int) temperature.size() / 2);
        return mediana;
    }

    public static float getPresionMediana() {
        init();
        float mediana = 0;
        Collections.sort(presion);
        mediana = (float) presion.get((int) presion.size() / 2);
        return mediana;
    }

    public static float getPluviosityMediana() {
        init();
        float mediana = 0;
        Collections.sort(pluviosity);
        mediana = (float) pluviosity.get((int) pluviosity.size() / 2);
        return mediana;
    }

    public static float getTemperatureVarianza() {
        init();
        float media = getTemperatureMedia();
        float varianza = 0;
        for (int i = 0; i < temperature.size(); i++) {
            varianza += (float) Math.pow(((float) temperature.get(i) - media), 2);
        }
        varianza = varianza / temperature.size();
        return varianza;
    }

    public static float getPresionVarianza() {
        init();
        float media = getPresionMedia();
        float varianza = 0;
        for (int i = 0; i < presion.size(); i++) {
            varianza += (float) Math.pow(((float) presion.get(i) - media), 2);
        }
        varianza = varianza / presion.size();
        return varianza;
    }

    public static float getPluviosityVarianza() {
        init();
        float media = getPluviosityMedia();
        float varianza = 0;
        for (int i = 0; i < pluviosity.size(); i++) {
            varianza += (float) Math.pow(((float) pluviosity.get(i) - media), 2);
        }
        varianza = varianza / pluviosity.size();
        return varianza;
    }

    public static float getTemperatureDE() {
        init();
        float varianza = getTemperatureVarianza();
        float DE = (float) Math.sqrt(varianza);
        return DE;
    }

    public static float getPresionDE() {
        init();
        float varianza = getPresionVarianza();
        float DE = (float) Math.sqrt(varianza);
        return DE;
    }

    public static float getPluviosityDE() {
        init();
        float varianza = getPluviosityVarianza();
        float DE = (float) Math.sqrt(varianza);
        return DE;
    }

    public static float getTemperatureRI() {
        init();
        float Q1 = (float) temperature.get((int) (temperature.size() + 3) / 4);
        float Q3 = (float) temperature.get((int) (3 * temperature.size() + 1) / 4);
        float RI = Q3 - Q1;
        return RI;
    }

    public static float getPresionRI() {
        init();
        float Q1 = (float) presion.get((int) (presion.size() + 3) / 4);
        float Q3 = (float) presion.get((int) (3 * presion.size() + 1) / 4);
        float RI = Q3 - Q1;
        return RI;
    }

    public static float getPluviosityRI() {
        init();
        float Q1 = (float) pluviosity.get((int) (pluviosity.size() + 3) / 4);
        float Q3 = (float) pluviosity.get((int) (3 * pluviosity.size() + 1) / 4);
        float RI = Q3 - Q1;
        return RI;
    }
}
